package com.dzx.swing;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;

/**
 * @author 段朝旭
 * @ClassName:
 * @Description:
 * @date 2019年07月25日 15:51:56
 */
public class SampeDemo {

    public static void main(String[] args) {
        new SampeDemo();
    }

    private JFrame frame = new JFrame("选择你的星座");
    private JPanel panel = new JPanel();
    private JComboBox cmb = new JComboBox(); //创建JComboBox
    private JLabel label = new JLabel("添加新星座");
    private JLabel showInfo = new JLabel();
    private JTextField jtf = new JTextField(16); // 用于显示信息
    private JButton buttonAdd = new JButton("新增");
    private JButton buttonDel = new JButton("删除");

    public SampeDemo() {
        //  JFrame frame = new JFrame("选择你的星座");
        cmb.addItem("--请选择--");
        cmb.addItem("巨蟹座");
        cmb.addItem("狮子座");
        cmb.addItem("双鱼座");
        panel.add(cmb);
        panel.add(label);
        panel.add(jtf);
        panel.add(buttonAdd);
        panel.add(buttonDel);
        frame.add(panel);
        buttonAdd.addActionListener(new MyActionListener());
        buttonDel.addActionListener(new MyActionListener());
        cmb.addItemListener(new MyItemListener());
        frame.setBounds(300, 200, 600, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    private class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if ("新增".equals(command)) {


                //判断文本框是否有值
                if (jtf.getText().length() != 0) {
                    boolean exist = false;
                    int itemCount = cmb.getItemCount();
                    for (int i = 0; i < itemCount; i++) {
                        String itemName = cmb.getItemAt(i).toString();
                        if (jtf.getText().equals(itemName)) {
                            exist = true;
                            break;
                        }
                    }
                    if (!exist) {
                        cmb.addItem(jtf.getText());
                        showInfo.setText("添加成功，新增了：" + jtf.getText());
                        panel.add(showInfo);
                    } else {
                        showInfo.setText("当前星座已存在，请勿重复添加！");
                        panel.add(showInfo);
                    }
                } else {

                    showInfo.setText("请输入要添加星座：");
                    panel.add(showInfo);
                }
            }
            if ("删除".equals(command)) {
                int i = JOptionPane.showConfirmDialog(frame, "确定要删除吗？", "删除提示", 0);
                System.out.println("删除与否：" + i);
               if(0 == i){
                if (cmb.getSelectedIndex() != -1) {

                    String s = cmb.getSelectedItem().toString();
                    if (!s.contains("请选择")) {
                        cmb.removeItem(s);
                        showInfo.setText("删除成功，删除了：" + s);
                        panel.add(showInfo);
                    } else {
                        showInfo.setText("当前项不允许删除！");
                        panel.add(showInfo);
                    }
                } else {
                    showInfo.setText("请选择要删除的星座：");
                    panel.add(showInfo);

                }
            }
            }

        }
    }

    private class MyItemListener implements java.awt.event.ItemListener {
        @Override
        public void itemStateChanged(ItemEvent e) {
            String s = e.getItem().toString();
            showInfo.setText("当前选中的星座是：" + s);
            panel.add(showInfo);
        }
    }
}
